package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.AdrefListQueryEntity;
import com.glory.brepda.entity.LotHoldEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class LotReservedActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvSuspendCode)
    TextView tvSuspendCode;
    @BindView(R.id.ivSuspendCodeArrow)
    ImageView ivSuspendCodeArrow;
    @BindView(R.id.flSuspendCodeList)
    FrameLayout flSuspendCodeList;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.etReason)
    FreeUI_ClearEditText etReason;
    @BindView(R.id.etComment)
    FreeUI_ClearEditText etComment;
    @BindView(R.id.tvQty)
    TextView tvQty;
    @BindView(R.id.tvPackageType)
    TextView tvPackageType;
    @BindView(R.id.tvType)
    TextView tvType;
    @BindView(R.id.tvStepDesc)
    TextView tvStepDesc;
    @BindView(R.id.tvLotState)
    TextView tvLotState;

    private FreeUI_GeneralPop codePop;
    private ArrayList<AdrefListQueryEntity.AdrefList> adreflist;
    private ArrayList<String> suspendCodeList = new ArrayList<>();
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lotreserved;
    }

    @Override
    protected void initView() {

        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvBaseSubmit.setText("提交");
        tvTitle.setText(title);
        setMainLayout(llMain);

    }

    @Override
    protected void initData() {

        initEdit(etLotID,s ->{
            LotUtils.searchLotInfo(LotReservedActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
                @Override
                public void onSuccess(LotInfoEntity response) {
                    tvQty.setText(response.getLOT().getMAINQTY());
                    tvPackageType.setText(response.getPACKAGETYPE());
                    tvType.setText(response.getPART().getSPEC2());
                    tvStepDesc.setText(response.getLOT().getSTEPDESC());
                    tvLotState.setText(response.getLOT().getSTATE());
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t, "LOTQUERY");
                }

            });
        });

        getSuspendCodeData();

    }

    @OnClick({R.id.tvBaseSubmit,R.id.flSuspendCodeList})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (getText(etComment).equals("")){
                    showToast("请输入原因",false);
                    return;
                }
                FreeUI_EntityCallBack<BaseResponseBean<Object>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<Object>>
                        (new TypeToken<BaseResponseBean<Object>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<Object>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                            showToast("暂停成功",true);
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<Object>> response) {
                        super.onError(response);
                        loadError(response.getException(),"TOOLBOMBASIC");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE","HOLD");
                map.addProperty("LOTID",getText(etLotID));
                String suspendCode = "";
                for (AdrefListQueryEntity.AdrefList data:adreflist){
                    if (data.getDESCRIPTION().equals(tvSuspendCode.getText().toString())){
                        suspendCode = data.getKEY();
                    }
                }
                map.addProperty("ACTIONCODE",suspendCode);
                map.addProperty("ACTIONREASON",getText(etReason));
                map.addProperty("ACTIONCOMMENT",getText(etComment));
                map.addProperty("COMMENT",getText(etComment));
                LotHoldEntity lotHoldEntity = new LotHoldEntity();
                map.add("LOTHOLD",new JsonParser().parse(new Gson().toJson(lotHoldEntity)).getAsJsonObject());
                NetUtils.requestPostNet("IF.LOTHOLD","Common", LotReservedActivity.this,map,callBack);

                break;
            case R.id.flSuspendCodeList:
                if (codePop != null) {
                    if (codePop.isShowing()) {
                        codePop.dismiss();
                    }
                } else {
                    codePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flSuspendCodeList.getWidth(), suspendCodeList, ivSuspendCodeArrow, (codeS, i) -> {
                        tvSuspendCode.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivSuspendCodeArrow);
                codePop.showAsDropDown(flSuspendCodeList, 0, 0);
                break;
        }
    }

    private void getSuspendCodeData() {
        FreeUI_EntityCallBack<BaseResponseBean<AdrefListQueryEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<AdrefListQueryEntity>>
                (new TypeToken<BaseResponseBean<AdrefListQueryEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<AdrefListQueryEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    adreflist = response.body().getResponse().getBody().getADREFLIST();
                    for (AdrefListQueryEntity.AdrefList data:adreflist){
                        suspendCodeList.add(data.getDESCRIPTION());
                    }

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<AdrefListQueryEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"ADREFLISTQUERY");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("REFERENCENAME","HoldCode");
        map.addProperty("REFERENCETYPE","USER");
        NetUtils.requestPostNet("IF.ADREFLISTQUERY","Common", LotReservedActivity.this,map,callBack);

    }

}
